<?php
	session_start();
	require('../Gen_Scripts/gen_functions.php');

	login();
	wipe_active();

	if($_POST['Add'] == 'Add'){

		$game_no = '';
		if($_POST['game_no'] != ''){
			$game_no = $_POST['game_no'];
		}

		$game = '';
		if($_POST['Game'] != ''){
			$game = $_POST['Game'];
		}


		
		$use_rsn1 = false;
		$rsn1 = $_POST['rsn1'];
		if($_POST['use_rsn1'] == 'use'){
			$use_rsn1 = true;
		}

		$use_rsn2 = false;
		$rsn2 = $_POST['rsn2'];
		if($_POST['use_rsn2'] == 'use'){
			$use_rsn2 = true;
		}
				
		$i=0;
		while($i < $_POST['cnt'] ){
			$i++;
			$item = 'player'.$i; $player = $_POST[$item];
			$item = 'Rsvp'.$i;	 $rsvp = $_POST[$item];
			$item = 'Sub'.$i;	 $sub = $_POST[$item];
			$item = 'Win'.$i;	 $win = $_POST[$item];
			$item = 'Rsn1_'.$i;	 $amt_rsn1 = $_POST[$item];
			$item = 'Rsn2_'.$i;	 $amt_rsn2 = $_POST[$item];
			$item = 'Guests'.$i; $guest = $_POST[$item];
			$item = 'bring_food'.$i; $bring_food = $_POST[$item];
			$item = 'bring_item'.$i; $bring_item = $_POST[$item];
			$item = 'max_gp';   $max_gp = $_POST[$item];

			if($use_rsn1){
				$amt_rsn1 = $amt_rsn1 * 30/$max_gp;	
			}
			if($use_rsn2){
				$amt_rsn2 = $amt_rsn2 * 30/$max_gp;	
			}


			if($bring_item == "clicked"){
				$amount = 2;
				$reason = "Brought item(s) for ".$game;
				$query = 
				"insert into GAMEPT (GAME_NO, PLAYER_ID, AMOUNT, REASON,TYPE) value
				 ($game_no, $player,$amount,'$reason','ITEM')";
				if($result = mysql_query($query)){
					echo 'Add GP for ITEM sucessful';
				}else{
					echo 'Failure: ',$query,'<br>';
				}
			}
						
			if($bring_food == "clicked"){
				$amount = 2;
				$reason = "Brought food for ".$game;
				$query = 
				"insert into GAMEPT (GAME_NO, PLAYER_ID, AMOUNT, REASON,TYPE) value
				 ($game_no, $player,$amount,'$reason','FOOD')";
				if($result = mysql_query($query)){
					echo 'Add GP for FOOD sucessful';
				}else{
					echo 'Failure: ',$query,'<br>';
				}
			}

			if($rsvp == "clicked"){
				$amount = 3;
				$reason = "Rsvped for ".$game;
				$query = 
				"insert into GAMEPT (GAME_NO, PLAYER_ID, AMOUNT, REASON,TYPE) value
				 ($game_no, $player,$amount,'$reason','RSVP')";
				if($result = mysql_query($query)){
					echo 'Add GP for RSVP sucessful';
				}else{
					echo 'Failure: ',$query,'<br>';
				}
			}
			
			if($win == "clicked"){
				$amount = 5;
				$reason = "Won the game: ".$game;
				$query = 
				"insert into GAMEPT (GAME_NO, PLAYER_ID, AMOUNT, REASON, TYPE) value
				 ($game_no, $player,$amount,'$reason','WIN')";
				if($result = mysql_query($query)){
					echo 'Add GP for WIN sucessful';
				}else{
					echo 'Failure: ',$query,'<br>';
				}
			}
			
			if($sub == "clicked"){
				$amount = 2;
				$reason = "Submitted played game: ".$game;
				$query = 
				"insert into GAMEPT (GAME_NO, PLAYER_ID, AMOUNT, REASON, TYPE) value
				 ($game_no, $player,$amount,'$reason','SUB')";
				if($result = mysql_query($query)){
					echo 'Add GP for SUB sucessful';
				}else{
					echo 'Failure: ',$query,'<br>';
				}
			}
			
			if($use_rsn1 and $amt_rsn1 != ''){
				$reason = $rsn1." in ".$game;
				$query = 
				"insert into GAMEPT (GAME_NO, PLAYER_ID, AMOUNT, REASON,TYPE) value
				 ($game_no, $player,$amt_rsn1,'$reason','GP')";
				if($result = mysql_query($query)){
					echo 'Add GP for RSN1 sucessful';
				}else{
					echo 'Failure: ',$query,'<br>';
				}
			}
			
			if($use_rsn2 and $amt_rsn2 != ''){
				$reason = $rsn2." in ".$game;
				$query = 
				"insert into GAMEPT (GAME_NO, PLAYER_ID, AMOUNT, REASON, TYPE) value
				 ($game_no, $player,$amt_rsn2,'$reason','GP')";
				if($result = mysql_query($query)){
					echo 'Add GP for RSN2 sucessful';
				}else{
					echo 'Failure: ',$query,'<br>';
				}
			}

			if($guest != ''){
				$guest_amt = $guest*5;
				$reason = "Brought ".$guest." guests to ".$game;
				$query = 
				"insert into GAMEPT (GAME_NO, PLAYER_ID, AMOUNT, REASON,TYPE) value
				 ($game_no, $player,$guest_amt,'$reason','GUEST')";
				if($result = mysql_query($query)){
					echo 'Add GP for GUEST sucessful';
				}else{
					echo 'Failure: ',$query,'<br>';
				}
			}
		}
	}
	
	
	if($_POST['del_sub'] == "update"){
		if($_POST['del'] == "clicked"){
			$query =
			"delete from db56987_fnt.bripp where bripot_id = ".$_POST['bripot'];
			$result = mysql_query($query);
		}
	}
	
?>
<script type="text/javascript">
$(document).ready(function(){
    // bind form using ajaxForm 
    $('#GPADD').ajaxForm({ 
        // target identifies the element(s) to update with the server response 
        target: '#gp_add_ajax', 
 
        // success identifies the function to invoke when the server response 
        // has been received; here we apply a fade-in effect to the new content 
        success: function() { 
            $('#gp_add_ajax').fadeIn('slow'); 
        }
    }); 
});
</script>

<form id="GPADD" name="GPADD" method="POST" action="Manage/gp_add.php">
	<label> Game No:
	<select name="game_no">
	<?php
		dblogin();
		$query=
		"select game_no from TOURNY";
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)){
			echo '<option value="',$row['game_no'],'">',$row['game_no'],'</option>';
		}	
	?>
	</select></label>	
	<label> Game:
	<select name="Game">
	<?php
		dblogin();
		$query=
		"select game, gameid from GAMES";
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)){
			echo '<option value="',$row['game'],'">',$row['game'],'</option>';
		}	
	?>
	</select></label>	
	<label> Use Reason 1:
	<input type="checkbox" name="use_rsn1" value="use"></label>
	<label> Reason 1
	<input type="text" name="rsn1" value=""></label>
	<label> Use Reason 2:
	<input type="checkbox" name="use_rsn2" value="use"></label>
	<label> Reason 2
	<input type="text" name="rsn2" value=""></label>
	<label> Max Gp </label>
	<input type="text" name="max_gp" value="">
		
	<table><thead>
		<tr>
		<th scope="col">Rsvp</th>
		<th scope="col">Sub</th>
		<th scope="col">Win</th>		
		<th scope="col">Bring Food</th>
		<th scope="col">Bring Item</th>
		<th scope="col">Player</th>
		<th scope="col">Amt Rsn1</th>
		<th scope="col">Amt Rsn2</th>
		<th scope="col">Num Guests</th>
		</tr>
	</thead>
	<tbody>
	
	<?php
	dblogin();
	$query = "select count(login) as \"CNT\" from PLAYERS";
	$result = mysql_query($query);
	$num_players = 0;
	if($row = mysql_fetch_array($result)){
		$num_players = $row['CNT'];
	}
	
	$query = "select player_id, login,name from PLAYERS";
	$result = mysql_query($query);
	$i = 0;
	while($row = mysql_fetch_array($result)){
		$i = $i+1;
		$player_id = $row['player_id'];
		$login = $row['login'];
		$name = $row['name'];
		
		
	?>	
	  <tr>
      <input type="hidden" name="cnt" value = "<?php echo $num_players; ?>">
      <input type="hidden" name="player<?php echo $i; ?>" value = "<?php echo $login; ?>">

	  <td><input type="checkbox" name="Rsvp<?php echo $i; ?>" value="clicked"></td>
	  <td><input type="checkbox" name="Sub<?php echo $i; ?>" value="clicked"></td>
	  <td><input type="checkbox" name="Win<?php echo $i; ?>" value="clicked"></td>
	  <td><input type="checkbox" name="bring_food<?php echo $i; ?>" value="clicked"></td>
	  <td><input type="checkbox" name="bring_item<?php echo $i; ?>" value="clicked"></td>
	  
      <td><input type="hidden" name="player<?php echo $i; ?>" value = "<?php echo $player_id; ?>">
	      <input type="text" name="<?php echo $i; ?>" value = "<?php echo $name; ?>"></td>
	  
	  
	  
	  <td><input type="text" name="Rsn1_<?php echo $i; ?>" value=""></td>
	  <td><input type="text" name="Rsn2_<?php echo $i; ?>" value=""></td>
	  <td><input type="text" name="Guests<?php echo $i; ?>" value=""></td>
	  </tr>

<?php
	  }
?>
</tbody></table>
<input class="button align-right" type="submit" name="Add" value="Add"  />
</form>
